import QtQuick 2.0

Rectangle
{
    id: imageBtn
    width: 80
    height: 80


    property bool m_bCheck: false
    //按钮名字
    property string m_strButtonName: ""
    //按钮图片路径
    property string m_strBtPicPath: "./../../Config/Skins/"+setting.m_skin+"/Icons/"
    property string m_strButtonId: ""
    //按钮base64编码图片(如果为空，则根据按钮id显示本地图片)
    property string m_str64CodeImageData:""

    property bool m_morePanel: false

    //按钮各个状态下的image
    property string m_strNormalImageSource:""
    property string m_strHoverImageSource: ""
    property string m_strPressImageSource:""
    property string m_strCheckImageSource: ""

    property alias hoverEnable : mousea.hoverEnabled

    //按钮各个状态下颜色
    property color m_normalbkcolor: g_Colors.m_cButtonNormal
    property color m_hoverbkcolor: g_Colors.m_cButtonHover
    property color m_pressedbkcolor: g_Colors.m_cButtonPress
    property color m_checkbkcolor: g_Colors.m_cButtonCheck
    property color m_colorInvalid: g_Colors.m_cButtonInvalid
    property color m_textInvalidcolor: g_Colors.m_cTextInvalid
    property color m_buttonTextColor: g_Colors.m_cButtonText
    property color m_buttonTextssColor: g_Colors.m_cButtonText


    property bool m_bTextVisible: false
    property string m_btnTextValue: ""

    //图片后缀
    property string m_strPrefix : "leftPanelBt_"
    property string m_strPostfixPress : "_Press.png"
    property string m_strPostfixNormal : "_Normal.png"
    property string m_strPostfixHover : "_Hover.png"
    property string m_strPostfixCheck : "_Check.png"

    property bool m_bIsFill : false

    //image尺寸
    property double m_fImageWidthPercent : 0.85
    property double m_fImageHeightPercent : 0.85
    property bool m_bHoverTextAtBottom: false
    property alias m_btnNameFontsize : textss.font.pixelSize
    property alias m_btnMidNameFontsize : name.font.pixelSize
    property alias m_btnMouseArea : mousea
    property bool m_setfillMode: false
    property bool m_btenable: true
    property bool m_bNotAFunction : false
    property bool m_btNoRespond : false
    property bool m_centerTextBold: false
    property bool m_neddDirection: false
    property string m_leaveDirection : "" //代表鼠标离开的方向,有LRUD四个方向
    property var m_lastPos : [0,0]


    property double m_textSize:m_bHoverTextAtBottom ? 0.14:0.15

    //tips相关定义
    property bool m_bTipsVisible:false
    property string m_sTipsText: ""
    property bool m_bNewButton:false
    property var tipsParent:null
    property alias m_nTipsX: tips.x
    property alias m_nTipsY: tips.y
    property alias m_nTipsHeight:tips.height
    property alias m_nTipsWidth:tips.width
    property alias m_nTipsname:tipsName

    //
    property bool m_bVerticalScreen: false

    onM_btenableChanged:
    {
        if ( m_btenable )
        {
            imageBtn.color = m_normalbkcolor
            name.color = m_buttonTextColor
            textss.color = m_buttonTextColor

            image.opacity = 1.0
        }
        else
        {
            imageBtn.color = m_colorInvalid
            name.color = m_textInvalidcolor
            textss.color = m_textInvalidcolor

            //具有功能的按钮图片会变暗
            if ( !m_bNotAFunction )
            {
                image.opacity = 0.5
            }
        }
    }

    onM_bIsFillChanged:
    {
        updateImageFilltype();
    }

    onWidthChanged:
    {
        updateImageFilltype();
    }

    onHeightChanged:
    {
        updateImageFilltype();
    }

    onM_normalbkcolorChanged:
    {
        imageBtn.color = m_bCheck ? m_pressedbkcolor : m_normalbkcolor
    }

    onM_strPrefixChanged:
    {
        updateImageinfo()
    }

    onM_strNormalImageSourceChanged:
    {
        image.source = m_bCheck ? updateImageSource(imageBtn.m_strCheckImageSource):updateImageSource(imageBtn.m_strNormalImageSource)
    }

    onM_bCheckChanged:
    {
        if ( m_bCheck )
        {
            image.source = updateImageSource( imageBtn.m_strCheckImageSource )
            imageBtn.color = m_pressedbkcolor
        }
        else
        {
            image.source = updateImageSource( imageBtn.m_strNormalImageSource )
            imageBtn.color = m_normalbkcolor
        }
    }

    function updateImageFilltype()
    {
        //
    }

    function updateImageSource ( strBtnImageSource)
    {
        if ( m_str64CodeImageData.length == 0)
        {
            return strBtnImageSource;
        }
        else
        {
            if ( m_str64CodeImageData.indexOf( "data:image/jpeg;base64," ) == -1)
            {
                return "data:image/jpeg;base64," + m_str64CodeImageData;
            }
            //使用64位编码数据展示
            return m_str64CodeImageData
        }
    }

    function updateImageinfo()
    {
        if ( m_strButtonId == "")
        {
            return;
        }

        m_strNormalImageSource = m_strBtPicPath +m_strPrefix+m_strButtonId +m_strPostfixNormal
        m_strHoverImageSource = getImageSourseFile(m_strPostfixHover)
        m_strPressImageSource = getImageSourseFile(m_strPostfixPress)
        m_strCheckImageSource = getImageSourseFile(m_strPostfixCheck)

        image.source = m_bCheck ? imageBtn.m_strCheckImageSource : imageBtn.m_strNormalImageSource
    }

    //获取图像资源路径，如果其他状态的文件不存在，则返回普通状态的图
    function getImageSourseFile(strFileType)
    {
        var filePath = m_strBtPicPath +m_strPrefix+m_strButtonId + strFileType;
        //        if (!interfaceCommon.isFileExist(filePath))
        //        {
        //            return m_strNormalImageSource;
        //        }

        return filePath;
    }

    //相关信号定义
    signal signalLeftClick();
    signal signalRightClick();
    signal signalMouseEnter();
    signal signalMouseExited();
    signal signalPressAndHold();
    signal signalPressBtn();

    Flow
    {
        spacing: Qt.platform.os == "ios"?3:6
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        flow: m_bHoverTextAtBottom ? Flow.TopToBottom : Flow.LeftToRight

        Item
        {
            width: m_strNormalImageSource == "" ? 0 : imageBtn.width
            height: m_strNormalImageSource == "" ? 0: imageBtn.height *m_fImageHeightPercent

            Image
            {
                id: image
                smooth: true
                source: {
                    return updateImageSource(imageBtn.m_strNormalImageSource);
                }

                width: imageBtn.width * m_fImageWidthPercent
                height: imageBtn.height * m_fImageHeightPercent
                fillMode: m_setfillMode ? Image.Stretch : Image.PreserveAspectFit
                visible: {
                    if(m_str64CodeImageData.length==0)
                    {
                        return imageBtn.m_strNormalImageSource==""?false:true
                    }
                    else
                    {
                        return imageBtn.m_str64CodeImageData==""?false:true
                    }
                }

                anchors.centerIn: parent
            }
        }

        LQBaseText
        {
            id: name
            width: text == "" ? 0:imageBtn.width
            height: text == "" ? 0:imageBtn.height
            text: m_btnTextValue
            color: m_buttonTextColor
            visible: m_btnTextValue != "" &&m_bTextVisible
            font.pixelSize: ( imageBtn.width >= imageBtn.height ? imageBtn.width:imageBtn.height) * m_textSize

            horizontalAlignment: m_textwh.contentWidth > width ? Text.AlignLeft:Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

    Item {
        id: imageMore
        width: imageMore.visible ? (m_bVerticalScreen ? image.height*0.3 : image.width * 0.2):0
        height: width * 0.9
        anchors.right: parent.right
        anchors.rightMargin: image.width * 0.25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: image.width * 0.35
        visible: m_morePanel
        z: 5

        Image
        {
            width: parent.width * 0.8
            height: width
            anchors.centerIn: parent
            smooth: true
            fillMode: Image.PreserveAspectFit
            source: "./../../Config/Skins/" + setting.m_skin + "/Icons/"+"BaseToolPanel/MorePanel.png"
        }
    }

    Rectangle
    {
        id: tips
        width: tipsName.width + 10
        height: tips.visible ? (m_bVerticalScreen?imageMore.height*1.5:imageMore.height*1.1):0
        x: tips.visible?(m_bVerticalScreen?(imageBtn.x+(imageBtn.width-tips.width)*0.5):1):0
        y: tips.visible?(m_bVerticalScreen?(parent.height-tips.height-1):(imageBtn.y+imageBtn.height*0.5-image.width*0.1-tips.height+5)):0
        color:setting.getPlatForm()===1?g_Colors.m_toolbarTipsColor:g_Colors.m_cNullBackgroundColor
        radius: 5
        visible: m_bTipsVisible

        LQBaseText
        {
            id: tipsName
            text: m_sTipsText
            color: m_buttonTextColor
            font.pixelSize: tips.height * 0.5
            anchors.centerIn: parent
        }
    }

    LQBaseText {
        id: textss
        anchors.bottom:  parent.bottom;
        anchors.left: parent.left;
        anchors.leftMargin: Qt.platform.os === "ios"?2:4;
        anchors.bottomMargin: Qt.platform.os === "ios"?2:4;
        width:
        {
            if(text == "")
            {
                return 0;
            }
            parent.width - (Qt.platform.os === "ios"?4:8)
        }
        text: qsTr(m_strButtonName)
        font.pixelSize: (imageBtn.width <= imageBtn.height ? imageBtn.width : imageBtn.height)*m_textSize
        color:m_buttonTextssColor
    }

    MouseArea {
        id: mousea
        anchors.fill: parent
        //桌面版才有hover状态变化
        hoverEnabled: setting.getPlatForm() == 1 ? true:false
        acceptedButtons: Qt.AllButtons

        onEntered:
        {
            mouseEnter();
        }

        onExited:
        {
            mouseExited();
        }

        onClicked:
        {
            if ( m_btenable )
            {
                image.source = updateImageSource( imageBtn.m_strPressImageSource )
                imageBtn.color = m_pressedbkcolor
            }

            if ( m_btNoRespond )
            {
                return false
            }

            if ( mouse.button == Qt.LeftButton )
            {
                imageBtn.signalLeftClick();
            }
            if ( mouse.button == Qt.RightButton )
            {
                imageBtn.signalRightClick();
            }
        }

        onPressAndHold:
        {
            if ( m_btNoRespond )
            {
                return false
            }

            imageBtn.signalPressAndHold();
        }

        onPressed:
        {
            if ( m_btNoRespond )
            {
                return false
            }

            imageBtn.signalPressBtn();
        }

        onReleased:
        {
            if(m_btenable)
            {
                if(m_bCheck)
                {
                    image.source = updateImageSource(imageBtn.m_strCheckImageSource)
                    imageBtn.color = m_checkbkcolor
                }
                else
                {
                    if(mouseX >= 0 && mouseX < imageBtn.width && mouseY >= 0 && mouseY < imageBtn.height)
                    {
                        image.source = updateImageSource(imageBtn.m_strHoverImageSource)
                        imageBtn.color = m_hoverbkcolor
                    }
                    else
                    {
                        image.source = updateImageSource(imageBtn.m_strNormalImageSource)
                        imageBtn.color = m_normalbkcolor
                    }
                }
            }
            if(m_btNoRespond)
            {
                return false;
            }
        }

        onPositionChanged:
        {
            if(m_neddDirection)
            {
                m_leaveDirection = "";

                m_leaveDirection += "R"
                if((m_lastPos[0] - mouse.x) >0)
                {
                    m_leaveDirection += "L"
                }
                else if((m_lastPos[0] - mouse.x) <0)
                {
                    m_leaveDirection += "R"
                }
                if((m_lastPos[1] - mouse.y) >0)
                {
                    m_leaveDirection += "U"
                }
                else if((m_lastPos[1] - mouse.y) <0)
                {
                    m_leaveDirection += "D"
                }
                m_lastPos =  [mouse.x , mouse.y]
            }
        }

        onCanceled:
        {
            name.m_bScrolling = false
            textss.m_bScrolling = false;
            if(m_btenable)
            {
                if(m_bCheck)
                {
                    image.source = updateImageSource(imageBtn.m_strCheckImageSource)
                    imageBtn.color = m_checkbkcolor
                }
                else
                {
                    if(setting.getPlatForm()=== 1)
                    {
                        if(mouseX >= 0 && mouseX < imageBtn.width && mouseY >= 0 && mouseY < imageBtn.height)
                        {
                            image.source = updateImageSource(imageBtn.m_strHoverImageSource)
                            imageBtn.color = m_hoverbkcolor
                        }
                        else
                        {
                            image.source = updateImageSource(imageBtn.m_strNormalImageSource)
                            imageBtn.color = m_normalbkcolor
                        }
                    }
                    else
                    {
                        image.source = updateImageSource(imageBtn.m_strNormalImageSource)
                        imageBtn.color = m_normalbkcolor
                    }
                }
            }
        }


        function mouseEnter()
        {
            name.m_bScrolling = true
            textss.m_bScrolling = true
            if ( m_btenable )
            {
                image.source = updateImageSource( imageBtn.m_strHoverImageSource )
                imageBtn.color = m_hoverbkcolor
            }

            if ( m_btNoRespond )
            {
                return false;
            }

            imageBtn.signalMouseEnter();
        }

        function mouseExited()
        {
            name.m_bScrolling = false
            textss.m_bScrolling = false;
            if(m_btenable)
            {
                if(m_bCheck)
                {
                    image.source = updateImageSource(imageBtn.m_strCheckImageSource)
                    imageBtn.color = m_pressedbkcolor
                }
                else
                {
                    image.source = updateImageSource(imageBtn.m_strNormalImageSource)
                    imageBtn.color = m_normalbkcolor
                }
            }
            if(m_btNoRespond)
            {
                return false;
            }
            imageBtn.signalMouseExited();
        }
    }
}
